<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>分配权限窗口</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="resources/css/public.css" media="all" />
    <link rel="stylesheet" type="text/css" href="zTree_v3/css/bootstrapStyle/bootstrapStyle.css">
    <script type="text/javascript" src="zTree_v3/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="zTree_v3/js/jquery.ztree.all.min.js"></script>
    <script type="text/javascript" src="resources/layui/layui.js"></script>
</head>
<body>
<div>
    <input type="hidden" id="roleIdHiden"/>
    <ul id="regionZTree" class="ztree"></ul>
    <button class="layui-btn" id="privilegeBtn">修改权限</button>
</div>
</body>
</html>
<script>
    $(function () {
        var setting = {
            view: {
                dblClickExpand: false,//双击节点时，是否自动展开父节点的标识
                showLine: true,//是否显示节点之间的连线
                fontCss:{'color':'black'},//字体样式函数
                selectedMulti: false //设置是否允许同时选中多个节点
            },
            check:{
                chkboxType: { "Y": "ps", "N": "ps" },
                chkStyle: "checkbox",//复选框类型
                enable: true //每个节点上是否显示 CheckBox
            },
            data: {
                simpleData: {//简单数据模式
                    enable:true,
                    idKey: "id",
                    pIdKey: "pid",
                    rootPId: null
                }
            },
            callback: {
                //onClick:zTreeBeforeExpand // 用于捕获父节点展开之前的事件回调函数，并且根据返回值确定是否允许展开操作
            }
        };
        $.ajax({
            url:"roles/selectAllPermission.do",
            type:"get",
            dataType:"json",
            success:function(data){
                zNodes=data;
                zTreeObj = $.fn.zTree.init($("#regionZTree"), setting, data);//初始化操作
                zTreeObj.expandAll(true);
                getChecked();
            }
        });
        function getChecked(){
            $.ajax({
                url:"/roles/getChecked.do",
                type:"get",
                data:{rid:$("#roleIdHiden").val()},
                success:function (res) {
                    var result=res;
                    for(var i=0;i<result.length;i++){
                        zTree=$.fn.zTree.getZTreeObj("regionZTree");
                        var node=zTree.getNodeByParam("id",result[i]);
                        if(node!=null){
                            zTree.checkNode(node,true);
                        }
                    }
                }
            });
        }
        //修改权限
        $("#privilegeBtn").click(function () {
            var rid=$("#roleIdHiden").val();
            zTree=$.fn.zTree.getZTreeObj("regionZTree");
            //获取树形菜单中被勾选的Node对象
            var nodes = zTree.getCheckedNodes(true);
            var perids=new Array();
            $(nodes).each(function () {
                perids.push(this.id);
            });
            $.ajax({
                type:"get",
                url:"/roles/assignRight.do",
                data:{rid:rid,perids:perids},
                dataType:"json",
                success:function(res) {
                    layui.use(['form','layer','laydate','table','laytpl'],function(){
                        var index1=parent.layer.getFrameIndex(window.name)
                        parent.layer.close(index1);
                    });
                }
            });
        });

    });

</script>